#!/usr/bin/env python

import sys
import os

CMD = 'sobjc4gcc'
SHORT_OPTS = {'-h':0, '-o':1}
SHORT_ARG_OPTS = ['-W', '-E', '-t', '-S', '-c', '-w', '-I', '-L', '-l', '-O']
COMPATIBLE_LONG_OPTS = ['-save-temps', '-ans', '-pedantic', '-pedantic-errors']
FLAGS = ['no-nil-receivers', 'no-strict-message', 'constant-string-class']
OBJC_FLAGS = ['profile', 'inline-cache', 'no-inline-cache',
         'dispatch-table', 'no-dispatch-table',
]

args = [CMD]
sys.argv.pop(0)
for i in xrange(0, len(sys.argv)):
    arg = sys.argv[i]
    if arg.startswith('-f'):
        body = arg[2:]
        if body in FLAGS:
            arg = '--' + body
        elif body.startswith('objc-') and body[5:] in OBJC_FLAGS:
            arg = '--' + body[5:]
        args.append(arg)
    elif arg == '-v':
        args.append(arg)
        args.append('--cc-verbose')
    elif arg in SHORT_OPTS:
        args.append(arg)
        n = SHORT_OPTS[arg]
        args.extend(sys.argv[i+1:i+n])
        i += n
    elif arg[:2] in SHORT_ARG_OPTS:
        args.append(arg)
    elif arg in COMPATIBLE_LONG_OPTS:
        args.append('-'+arg)
    elif arg.startswith('-'):
        args.append('-X')
        args.append(arg)
    else:
        args.append(arg)

os.execvp(CMD, args)

